<?php
namespace Web\Controller;
use Think\Controller;
class WchatController extends Controller {

	private $id,$secret,$token;
     public function _empty($name){
        //把所有城市的操作解析到city方法
        $this->nul($name);
    }
    
    //注意 city方法 本身是 protected 方法
    protected function nul($name){
        //和$name这个城市相关的处理
         echo "<h1>404</h1>";
         echo "没有这个地址:".$name;
    }
    /**
     * 暂时验证js_sdk使用
     * @return [type] [description]
     */
	public function active(){
		$token = $this->token; //微信后台填写的TOKEN
		$appid=$this->id;
		$appsecret=$this->secret;
		$api=new \Com\WechatAuth($appid,$appsecret);
		$res=$api->new_jsapi_ticket();
		$string1="jsapi_ticket=".$res['ticket']."&noncestr=Wm3WZYTPz0wzccnW&timestamp=1447334894&url=http://pgl.oaio.online/index.php/Web/Wchat/js_sdk";
		$signature=sha1($string1);
		$this->ajaxReturn($signature);
	}
	/**
	 * 获取微信unionID
	 * @DateTime:    [2018-03-23 11:10:40]
	 */
	public function get_unionID(){
		$token = $this->token; //微信后台填写的TOKEN
		$appid=$this->id;
		$appsecret=$this->secret;
		$api=new \Com\WechatAuth($appid,$appsecret);
		$re=$api->getUserInfo('oaq2W1eUKTv62TUhvOoi5nE8qKrs');
		dump($re);
	}



	/**
	 * @DateTime:    [2018-02-25 11:05:27]
	 * 获取微信js_sdk签名和数据
	 */
	public function get_wechat_jssdk(){	
		// $token = $this->token; //微信后台填写的TOKEN
		// $appid=$this->id;
		// $appsecret=$this->secret;
		// $api=new \Com\WechatAuth($appid,$appsecret);

		// $res=$api->new_jsapi_ticket();
		// $ticket="jsapi_ticket=".$res."&noncestr=".C('WECHAT_JS_CONFIUG.noncestr')."&timestamp=".C('WECHAT_JS_CONFIUG.timestamp')."&url=".C('WECHAT_JS_CONFIUG.url');

		// $msg['data']=C('WECHAT_JS_CONFIUG');
		// $msg['data']['ticket']=$ticket;
		// $msg['data']['sign']=sha1($ticket);
		// $msg['data']['appid']=$this->id;
		// $msg['code']=1;
		// $msg['message']='请求成功';
		// $this->ajaxReturn($msg);
       if(IS_AJAX&&IS_POST){
       	 if($_POST['wechta_id']){
       	 	$where['wechat_id']=$_POST['wechat_id'];
       	 	$r=M('Wechat')->where($where)->count();
       	 	if($r){
				$token = $this->token; //微信后台填写的TOKEN
				$appid=$this->id;
				$appsecret=$this->secret;
				$api=new \Com\WechatAuth($appid,$appsecret);

				$res=$api->new_jsapi_ticket();
				$ticket="jsapi_ticket=".$res."&noncestr=".C('WECHAT_JS_CONFIUG.noncestr')."&timestamp=".C('WECHAT_JS_CONFIUG.timestamp')."&url=".C('WECHAT_JS_CONFIUG.url');

				$msg['data']=C('WECHAT_JS_CONFIUG');
				$msg['data']['ticket']=$ticket;
				$msg['data']['sign']=sha1($ticket);
				$msg['data']['appid']=$this->id;
				$msg['code']=1;
				$msg['message']='请求成功';
       	 	}
       	 	else{
		       	 $msg['code']=2;
		       	 $msg['message']='传输方式错误1';
       	 	}
       	 }
       	 else{
	       	 $msg['code']=3;
	       	 $msg['message']='传输方式错误2';
       	 }
       }
       else{
       	  $msg['code']=4;
       	  $msg['message']='传输方式错误3';
       }
      $this->ajaxReturn($msg);
	}



	/*
	 * 用户登录,被动回复(默认)
	 * 
	 */
	public function _initialize(){
		header("Content-Type: text/html; charset=utf-8");
		$wechat= M('wechat')->where($_GET)->find();
		$_SESSION['wechat']=$wechat;
		$this->token = $wechat['token']; //微信后台填写的TOKEN
		$this->secret=$wechat['appsecret'];
		$this->id=$wechat['appid'];
	}
	/**
	 * 登陆检测默认绑定状态
	 * @return [type] [description]
	 */
	public function login(){
		unset($_SESSION['role']);
		unset($_SESSION['chat']);
		unset($_SESSION['shenqing']);
		unset($_SESSION['my_search_data']);
		unset($_SESSION['search_data']);
		unset($_SESSION['my_kuhu']);
		unset($_SESSION['my_lccdc']);
		if($_SESSION['wechat_data']&&$_SESSION['wechat_data'][0]['openid']){
			$where_temp_ouid['openid']=$_SESSION['wechat_data'][0]['openid'];
			$where_temp_ouid['unionid']=$_SESSION['wechat_data'][0]['unionid'];
			$where_temp_ouid['_logic']='or';
			$where['_complex']=$where_temp_ouid;
			$res=M('wechat_bind')->where($where)->find();
			if($res){
				$where1['bind_id']=$res['bind_id'];
				$user=M('web_user')->where($where1)->find();
				if($user){
					$_SESSION['user']=$user;
					$_SESSION['wechat_state']['code']=1;
					$_SESSION['wechat_state']['message']="绑定登陆中";
					$appid=$_SESSION['wechat_data'][1]['appid'];
					$appsecret=$_SESSION['wechat_data'][1]['appsecret'];
					$api=new \Com\WechatAuth($appid,$appsecret);
					$res=$api->userGet();
					$openid=$_SESSION['wechat_data'][0]['openid'];
					$res1=$api->userInfo($openid);
					/*********************微信头像存储*********************/
					if($res1['headimgurl']){
						$img_data['web_img']=$res1['headimgurl'];
						M('web_user')->where('web_id='.$_SESSION['user']['web_id'])->data($img_data)->save();
					}
					/*********************微信头像存储*********************/
					$this->assign('headimgurl',$res1['headimgurl']);
					$this->assign('user',$_SESSION['user']);
					switch ($user['web_type']){
						case 0:

							$this->display('WebUser/main');
							break;  
						case 1:
							$this->display('WebUser/main');
							break;  
						case 2:
						    $res_o=D('WebUser')->yh_goods_list();
							$api=new \Com\WechatAuth($appid,$appsecret);
							$openid=$_SESSION['wechat_data'][0]['openid'];
							$res1=$api->userInfo($openid);
							$this->assign('goods',$res_o);
							/*********************微信头像存储*********************/
							if($res1['headimgurl']){
								$img_data['web_img']=$res1['headimgurl'];
								M('web_user')->where('web_id='.$_SESSION['user']['web_id'])->data($img_data)->save();
							}
							/*********************微信头像存储*********************/
							$this->assign('headimgurl',$res1['headimgurl']);
							$this->assign('user',$_SESSION['user']);
							$this->display('WebUser/diqu_weixin');
							break;  
						default:
					}
				}
				else{
					$this->display('Wchat/login');die;
				}
			}
			else{
				$this->display('Wchat/login');die;
			}
			//session_destroy();
		
		}
		else{
			unset($_SESSION['wechat_data']);
			$this->code();
		}


	}
	/**
	 * 登陆分离判断
	 */
    /**
     * main
     */
    public function main(){
    	
		$appid=$_SESSION['wechat_data'][1]['appid'];
		$appsecret=$_SESSION['wechat_data'][1]['appsecret'];
		$api=new \Com\WechatAuth($appid,$appsecret);
		$res=$api->userGet();
		$openid=$_SESSION['wechat_data'][0]['openid'];
		$res1=$api->userInfo($openid);
		/*********************微信头像存储*********************/
		if($res1['headimgurl']){
			$img_data['web_img']=$res1['headimgurl'];
			M('web_user')->where('web_id='.$_SESSION['user']['web_id'])->data($img_data)->save();
		}
		/*********************微信头像存储*********************/
		$this->assign('headimgurl',$res1['headimgurl']);
		$this->assign('user',$_SESSION['user']);
		unset($_SESSION['role']);
		unset($_SESSION['chat']);
		unset($_SESSION['shenqing']);
		unset($_SESSION['my_search_data']);
		unset($_SESSION['search_data']);
		unset($_SESSION['my_kuhu']);
		unset($_SESSION['my_lccdc']);
    	$this->display('WebUser/main');
    }
	/**
	 * 微信绑定登录用户登陆的账号密码正确性
	 * @return [type] [description]
	 */
	public function wechat_login(){
		$m = M('web_user');
		$phone = $_POST['web_phone'];
		//加密登陆处理
/*		$result=passwd($_POST['web_phone'],$_POST['web_pass']);
		if($result['code']==1){
			$Login=$result['data'];
		}
		else{
			$this->error("手机号或邀请码不能为空");

		}*/
		/********注销线**************************/
		$pass = $_POST['web_pass'];
		$where0['web_pass']=$_POST['web_pass'];
		$where0['web_phone']=$_POST['web_phone'];
		if($phone == '' || $pass == ''){
			$this->error("手机号或密码不能为空");exit;
		}
		else{
			 $wherex['web_phone']=$_POST['web_phone'];
			 $my_web_user=M('web_user')->where($wherex)->find();
			 if($my_web_user['bind_id']>0){
			 	$this->error("账号已经被绑定了");exit;
			 }
		}
		$Login = $m->where($where0)->find();
		
		/********注销线**************************/
		if($Login){
			 session('user',$Login);
			/********************************绑定登陆处理*******************************/
			if($_POST['state']==1&&!$Login['bind_id']){
				$add_data['openid']=$_SESSION['wechat_data'][0]['openid'];
				$add_data['unionid']=$_SESSION['wechat_data'][0]['unionid'];
				$add_data['appid']=$_SESSION['wechat_data'][1]['appid'];
				$add_data['wechat_id']=$_SESSION['wechat_data'][1]['wechat_id'];
				$res=M('wechat_bind')->data($add_data)->add();
				if($res){
					$where_bind['web_id']=$Login['web_id'];
					$update_bind['bind_id']=$res;
					$result_bind=M('web_user')->where($where_bind)->save($update_bind);
					if($result_bind){
						
						/****************绑定成功处理*************************/
							$_SESSION['wechat_state']['code']=1;
							$_SESSION['wechat_state']['message']="绑定登陆中";
						/****************绑定成功处理*************************/
					}
					else{
						session_destroy();
						$this->redirect('Wchat/login','','0');exit;
					}
				}
				else{
					session_destroy();
					$this->redirect('Wchat/login','','0');exit;
				}
			}
			/********************************绑定登陆处理*******************************/
			if(!isset($_SESSION['wechat_state'])){
					$_SESSION['wechat_state']['code']=0;
					$_SESSION['wechat_state']['message']="未绑定登陆";
			}
			$appid=$_SESSION['wechat_data'][1]['appid'];
			$appsecret=$_SESSION['wechat_data'][1]['appsecret'];
			$api=new \Com\WechatAuth($appid,$appsecret);
			$res=$api->userGet();
			$openid=$_SESSION['wechat_data'][0]['openid'];
			$res1=$api->userInfo($openid);
			/*********************微信头像存储*********************/
			if($res1['headimgurl']){
				$img_data['web_img']=$res1['headimgurl'];
				M('web_user')->where('web_id='.$_SESSION['user']['web_id'])->data($img_data)->save();
			}
			/*********************微信头像存储*********************/
			$this->assign('headimgurl',$res1['headimgurl']);
			$this->assign('user',$_SESSION['user']);
			switch ($Login['web_type']){
				case 0:
					$this->display('WebUser/main');
					break;  
				case 1:
					$this->display('WebUser/main');
					break;  
				case 2:
					$this->redirect('Wchat/diqu_weixin','','0',' ');exit;
					break;  
				default:
			}
			
		}else{
			$this->error('账号或密码错误,请检查');exit;
		}
		

	}
	/**
	 * 银行职员微信首页
	 */
	
	public function diqu_weixin(){
	    $res_o=D('WebUser')->yh_goods_list();
		$appid=$_SESSION['wechat_data'][1]['appid'];
		$appsecret=$_SESSION['wechat_data'][1]['appsecret'];
		$api=new \Com\WechatAuth($appid,$appsecret);
		$openid=$_SESSION['wechat_data'][0]['openid'];
		$res1=$api->userInfo($openid);
		$this->assign('goods',$res_o);
		$this->assign('headimgurl',$res1['headimgurl']);
		$this->assign('user',$_SESSION['user']);
		$this->display('WebUser/diqu_weixin');
	}


    /**
     * 注册
     * post 注册数据
     * get 区分类型 $_GET['type']  1 预留 2 第一批银行职员3子账号银行职员 4第一批业务员 5业务员
     * 
     * @return [type] [description]
     */
    public function signup(){
        if($_POST){
			$m = M('web_user');
			$date = date("Y-m-d H:i:s");
			$user['web_phone']=$_POST['web_phone'];
			$user['web_name']=$_POST['web_name'];
			$user['web_pass']=$_POST['web_pass'];
			$user['code']=rand(10000,99999);
			$user['web_time'] = $date;

			/**************************类型归宿处理****************************************/
			/**
			 * 统一处理银行职员注册u推荐人q渠道
			 */
			
			switch ($_POST['type']) {
				case '1':
					if($_POST['q']=='ajy'){
						$where_f_user['u_id']=$_POST['u'];
						$f_user=M('user')->where($where_f_user)->find();
						$user['web_pro_id']=$f_user['u_pro_id'];
						$user['web_city_id']=$f_user['u_city_id'];
						$user['web_area_id']=$f_user['u_area_id'];
						$user['p_u_id']=$f_user['u_id'];
						$user['web_link_data']='-0-';
						$user['wechat_id']=$f_user['wechat_id'];
						$user['web_type']=1;
					}
					else{
						$where_f_user['u_id']=$_POST['u'];
						$f_user=M('user')->where($where_f_user)->find();
						if(!$_POST['u_pro_id']&&!$_POST['u_city_id']&&!$_POST['u_area_id']){
							$user['web_pro_id']=$f_user['u_pro_id'];
							$user['web_city_id']=$f_user['u_city_id'];
							$user['web_area_id']=$f_user['u_area_id'];
						}
						else{
							$user['web_pro_id']=re_region($_POST['u_pro_id']);
							$user['web_city_id']=re_region($_POST['u_city_id']);
							$user['web_area_id']=re_region($_POST['u_area_id']);
						}
						$user['p_u_id']=$_POST['u'];
						$user['wechat_id']=$f_user['wechat_id'];
						$user['qudao_id']=$_POST['q'];
						$user['web_type']=2;

					}
					break;
				case '2':
					$where_f_user['u_id']=$_POST['code'];
					$f_user=M('user')->where($where_f_user)->find();
					$user['web_pro_id']=$f_user['u_pro_id'];
					$user['web_city_id']=$f_user['u_city_id'];
					$user['web_area_id']=$f_user['u_area_id'];
					$user['p_u_id']=$f_user['u_id'];
					$user['wechat_id']=$f_user['wechat_id'];
					$user['web_link_data']='-0-';
					$user['web_type']=0;
					break;
				case '3':
					break;
				case '4':

					$where_f_user['u_id']=$_POST['code'];
					$f_user=M('user')->where($where_f_user)->find();
					$user['web_pro_id']=$f_user['u_pro_id'];
					$user['web_city_id']=$f_user['u_city_id'];
					$user['web_area_id']=$f_user['u_area_id'];
					$user['p_u_id']=$f_user['u_id'];
					$user['wechat_id']=$f_user['wechat_id'];
					$user['web_link_data']='-0-';
					$user['web_type']=0;
					break;
				case '5':
				    
					$where_f_user['web_id']=$_POST['code'];
					$f_user=M('web_user')->where($where_f_user)->find();
					$user['web_pro_id']=$f_user['web_pro_id'];
					$user['web_city_id']=$f_user['web_city_id'];
					$user['web_area_id']=$f_user['web_area_id'];
					$user['p_u_id']=$f_user['p_u_id'];
					$user['p_id']=$f_user['web_id'];
					$user['wechat_id']=$f_user['wechat_id'];
					$user['web_link_data']=$f_user['web_link_data'].$f_user['web_id'].'-';
					$user['web_type']=0;
					break;
				default:
					
					break;
			}
			/**************************类型归宿处理****************************************/
			$info = $m->data($user)->add();

			if($info){
	/*			$my_user= $m->where("web_id=$info")->find();
				$user_update['web_pass']=md5(md5($_POST['web_pass']).$my_user['code']);//MD5加密
				$m->where("web_id=$info")->save($user_update);*/
				//$this->ajaxReturn('ok');
				if($_POST['web_email']){
					$message="恭喜".$_POST['web_name']."成为评估令用户";
					send_email($_POST['web_email'],$message);
				}
				$this->display('Wchat/login');
			}else{
				$this->display('Wchat/login');
			}
        }
        else{
			switch ($_GET['type']) {
				case '1':
						$where_f_user['u_id']=$_GET['u'];
						$f_user=M('user')->where($where_f_user)->find();
						$user['wechat_id']=$f_user['wechat_id'];
					break;
				case '4':
					$where_f_user['u_id']=$_GET['code'];
					$f_user=M('user')->where($where_f_user)->find();
					$user['wechat_id']=$f_user['wechat_id'];
					break;
				case '5':
					$where_f_user['web_id']=$_GET['code'];
					$f_user=M('web_user')->where($where_f_user)->find();
					$user['wechat_id']=$f_user['wechat_id'];
					break;
			}
			 $this->assign('this_wechat_id',$user['wechat_id']);
			 $father_user=M('user')->where("u_id={$_GET['u']}")->find();
			 $GET_DATA['gb_u_area_id']=re_region($father_user['u_area_id']);
			 $GET_DATA['gb_u_city_id']=re_region($father_user['u_city_id']);
			 $GET_DATA['gb_u_pro_id']=re_region($father_user['u_pro_id']);
			 $this->assign('user_data',json_encode($GET_DATA));
        	 $this->assign('message',$_GET);
             $this->display();
        }
    }
    /**
     * ajax在注册前校验推荐码是否有效
     * @return [type] [description]
     */
    public function ajax_ck_fuser(){
    	if(IS_AJAX&&IS_POST){
    		$where['web_id']=$_POST['p_id'];
    		$res=M('web_user')->where($where)->find(); 
    		if($res){
	    		$msg['code']=1;
	    		$msg['message']='存在推荐用户';
	    		$msg['data']=$res;	
    		}
    		else{
	    		$msg['code']=2;
	    		$msg['message']='不存在推荐用户';
    		}

    	}
    	else{
    		$msg['code']=3;
    		$msg['message']='校验方式有误';
    	}
    	$this->ajaxReturn($msg);
    }
    /**
     * 客服二维码
     */
    
    public function huoquyaoqin(){
    	$this->display();
    }


    /**
     * 解除绑定
     * @return [type] [description]
     */
    public function rebind(){

    }

    /**
     * 退出
     */
    public function login_out(){
    	session_destroy();
    	$this->display('Wchat/login');
    }
	/*
	 * 微信获取code换取当前用户信息
	 * Enter description here ...
	 */
    public  function code(){
	  $APPID=$this->id;
	  $REDIRECT_URI='http://pgl.oaio.online/index.php/Web/Wchat/getcode?appid='.$APPID."&&appsecret=".$this->secret.'&&wechat_id='.$_SESSION['wechat']['wechat_id'];
	//$REDIRECT_URI='2508b1b1.nat123.cc/wechat/ncp/ncp.php/Home/Wchat/getcode';
	//$scope='snsapi_base';//不需要授权
    $scope='snsapi_userinfo';//需要授权
	$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
	header("Location:".$url);
    	
    }
    
    
   /*
    * 利用code获取access_token等个人用户信权
    *
    *
    *
    * 
    */
      public function  getcode(){
    		$wechat_id=$_GET['wechat_id'];
			$appid=$_GET['appid'];
			$appsecret=$_GET['appsecret'];
			$code = $_GET["code"]; 
			$get_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code ";
			$ch = curl_init($get_token_url);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_HEADER, true); 
			$res=curl_exec($ch);
			curl_close($ch);
			$res=strstr($res, "{");
			$_SESSION['wechat_data'][0]=json_decode($res,true);
			$_SESSION['wechat_data'][1]=$_GET;
			/**
			 * 获取微信唯一id
			 */
			$api=new \Com\WechatAuth($appid,$appsecret);
			$_SESSION['wechat_data'][2]=$api->getUserInfo($_SESSION['wechat_data'][0]['openid']);
			$this->login();
	}

	/**
	 * ajax获取渠道列表
	 */
		public function get_qudao_list(){
			if(IS_AJAX&&IS_POST){
				$where['qudao_pro_id']=re_region($_POST['qudao_pro_id']);
				$where['qudao_city_id']=re_region($_POST['qudao_city_id']);
				//$where['qudao_area_id']=re_region($_POST['qudao_area_id']);
				$qudao_list=M('qudao')->where($where)->select();
				if($qudao_list){
					$msg['code']=1;
					$msg['message']='请求成功';
					$msg['data']=$qudao_list;
				}
				else{
					$msg['code']=2;
					$msg['message']='没有指定数据';
				}

			}
			else{
				$msg['code']=3;
				$msg['message']='请求错误';
			}
			$this->ajaxReturn($msg);
		}
	/**
	 * 微信js_sdk
	 */
	public function js_sdk(){
		$this->display();
	}

}